package com.question.dicegame;

import java.awt.Graphics;
import java.awt.Image;

public class Player {

	
	private Image image;//표시될 이미지
	private int x, y;//위치 정보
	private int dx, dy;//이동량
		
	private int sliceCount1 = 4;
	private int sliceCount2 = 3;
	private int imageWidth;
	private int imageHeight = 80;
	private int currentImagePosition;
	private int direction;//0:up, 1:down, 3:right, 4, left
	private long speed = 500;
	private long lastAnimatedTime = System.currentTimeMillis();
	
	public Player(Image image, int sliceCount) {
		this.image = image;
		y = 0;
		x = 0;
		
		this.imageWidth = image.getWidth(null) / sliceCount;
		this.currentImagePosition = 0;
		this.direction = 2;
	}

	public void draw(Graphics g,int number) {

		//g.drawImage(image, x, y,null);
		
		int sx = imageWidth * currentImagePosition;
		int sy = imageHeight * direction;		
		
		g.drawImage(image, 
			x,					//이미지를 그릴 left 좌표 
			y, 					//이미지를 그릴 top 좌표
			x + imageWidth, 	//이미지를 그릴 right 좌표
			y + imageHeight, 	//이미지를 그릴 bottom 좌표
			sx, 				//원본 이미지에서 추출할 left 좌표
			sy, 				//원본 이미지에서 추출할 top 좌표
			sx + imageWidth, 	//원본 이미지에서 추출할 right 좌표
			sy + imageHeight, 	//원본 이미지에서 추출할 bottom 좌표
			null);
		
		if (number != 0) {			
			long current = System.currentTimeMillis();
			if (current - lastAnimatedTime > speed) {
				lastAnimatedTime = current;
				currentImagePosition++;
				
				if (number == 0|| dx!=0 || currentImagePosition >= sliceCount1)
					currentImagePosition = 0;
				if (number == 0|| dy!=0 || currentImagePosition >= sliceCount2)
					currentImagePosition = 0;
			}
		} 
	}
	
	public void update() {
		x += dx;
		y += dy;
		
		/*if (number != 0) {
			if (flag == 0) {
				movePaint(flag,number);
				moveEvent(flag);
				flag = 1;
			} else if (flag == 1) {
				movePaint(flag,number);
				moveEvent(flag);
				flag = 0;
			}
		}*/

	}

/*	public void movePaint(int flag,int number) {
		if (number != 0) {
			for (int i = 0; i < number*8; i++) {
				if (x == 10*imageWidth
						&& (y == 1*imageWidth || y == 2*imageWidth)) {
					x = 0;
					y = 0;
				} else if (x < 9*imageWidth && y == 0*imageWidth) {
					dx = 10;
				} else if (x == 9*imageWidth && y == 0*imageWidth) {
					y++;
				} else if (x > 0*imageWidth && y == 1*imageWidth) {
					x--;
				} else if (x == 0*imageWidth && y == 1*imageWidth) {
					y++;
				} else if (x < 9*imageWidth && y == 2*imageWidth) {
					x++;
				} else if (x == 9*imageWidth && y == 2*imageWidth) {
					y++;
				} else if (x > 0*imageWidth && y == 3*imageWidth) {
					x--;
				} else if (x == 0*imageWidth && y == 3*imageWidth) {
					y++;
				} else if (x < 9*imageWidth && y == 4*imageWidth) {
					x++;
				} else if (x == 9*imageWidth && y == 4*imageWidth) {
					y++;
				} else if (x > 0*imageWidth && y == 5*imageWidth) {
					x--;
				} else if (x == 0*imageWidth && y == 5*imageWidth) {
					y++;
				} else if (x < 9*imageWidth && y == 6*imageWidth) {
					x++;
				} else if (x == 9*imageWidth && y == 6*imageWidth) {
					y++;
				} else if (x > 0*imageWidth && y == 7*imageWidth) {
					x--;
				} else if (x == 0*imageWidth && y == 7*imageWidth) {
					y++;
				} else if (x < 9*imageWidth && y == 8*imageWidth) {
					x++;
				} else if (x == 9*imageWidth && y == 8*imageWidth) {
					y++;
				} else if (x > 0*imageWidth && y == 9*imageWidth) {
					x--;
				}
			}
			number = 0;
		}

	}

	private void moveEvent(int flag) {
		// 사다리 이벤트
		if (x == 5 && y == 0) {
			x = 8;
			y = 4;
		} else if (x == 9 && y == 1) {
			x = 9;
			y = 2;
		} else if (x == 4 && y == 2) {
			x = 2;
			y = 7;
		} else if (x == 1 && y == 4) {
			x = 0;
			y = 5;
		} else if (x == 9 && y == 5) {
			x = 9;
			y = 8;
		} else if (x == 8 && y == 9) {
			x = 5;
			y = 9;
		}
		// 꼬리 이벤트
		if (x == 2 && y == 9) {
			x = 2;
			y = 1;
		} else if (x == 3 && y == 7) {
			x = 8;
			y = 1;
		} else if (x == 8 && y == 7) {
			x = 4;
			y = 3;
		}
		if (x[0] == 0*imageWidth && y[1] == 9*imageWidth) {
			running = false;
		}
	}
	*/
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}
	public int getWidth() {
		return image.getWidth(null);
	}
	public int getHeight() {
		return image.getHeight(null);
	}
	
}
